package com.example.service.Impl;
import com.example.entity.Attendance;
import com.example.entity.Travel;
import com.example.entity.dto.AttendanceDto;
import com.example.entity.dto.AttendanceSearch;
import com.example.entity.vo.AttDataVO;
import com.example.entity.vo.AttVO;
import com.example.entity.vo.AttendanceVO;
import com.example.mapper.AttendanceMapper;
import com.example.service.AttendanceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TimeZone;


@Service
public class AttendanceServiceImpl implements AttendanceService {

    @Autowired
    private AttendanceMapper attendanceMapper;


    /**
     * 添加今日考勤
     * @param attendanceDto
     */
    public void addAttendance(AttendanceDto attendanceDto) {
        // 格式化日期为 'YYYY-MM-DD'
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = dateFormat.format(attendanceDto.getTime());
        for(int i = 0;i < attendanceDto.getEmployeeIds().size();i++){
            Attendance attendance = attendanceMapper.find(attendanceDto.getEmployeeIds().get(i), formattedDate);
            if(attendance == null){
                attendance = new Attendance();
                attendance.setEmployeeId(attendanceDto.getEmployeeIds().get(i));
                BeanUtils.copyProperties(attendanceDto,attendance);
                attendanceMapper.add(attendance);
            }else {
                BeanUtils.copyProperties(attendanceDto,attendance);
                attendanceMapper.updateAtt(attendance);
            }
        }
    }

    /**
     * 查看考勤
     * @param id
     * @return
     */
    public AttendanceVO findAtt(int id) {
        return attendanceMapper.findAtt(id);
    }

    /**
     * 查看当日考勤
     * @param date
     * @return
     */
    public List<AttendanceVO> getList(Date date) {

        return attendanceMapper.findAllAtt(date);
    }

    /**
     * 修改具体考勤
     * @param attendance
     * @return
     */
    public void updateAtt(Attendance attendance) {
        attendanceMapper.updateAtt(attendance);
    }

    /**
     * 删除考勤
     * @param id
     */
    public void deleteAtt(int id) {
        attendanceMapper.deleteAtt(id);
    }

    /**
     * 综合查询
     * @param attendanceSearch
     * @return
     */
    public List<AttendanceVO> search(AttendanceSearch attendanceSearch) {
        System.out.println(attendanceSearch);
        if(attendanceSearch.getEmployeeId() == 0 && attendanceSearch.getProjectId() == 0 && attendanceSearch.getTime() == null){
            return null;
        }
        return attendanceMapper.search(attendanceSearch);
    }

    /**
     * 获取员工当月工作考勤
     * @param employeeId
     * @param date
     * @return
     */
    public List<AttDataVO> findAttWorkingData(int employeeId, String date) {
        return attendanceMapper.findAttWorkingData(employeeId,date);
    }

    /**
     * 获取员工当月加班考勤
     * @param employeeId
     * @param date
     * @return
     */
    public List<AttDataVO> findAttOverData(int employeeId, String date) {
        return attendanceMapper.findAttOverData(employeeId,date);
    }

    /**
     * 获取总结当月考勤
     * @param employeeId
     * @param date
     * @return
     */
    public AttVO findAttData(int employeeId, String date) {
        return attendanceMapper.findAttData(employeeId,date);
    }


}
